package com.stox.ui.javafx.charting.widget.axis;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javafx.geometry.Rectangle2D;

import com.stox.ui.javafx.charting.model.plot.BarPrimaryPlot;
import com.stox.ui.javafx.charting.model.uiunit.BarPrimaryUiUnit;

public class DateAxis extends AbstractHorizontalAxis {
	
	private static final String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	
	private final List<Rectangle2D> rectangles = new ArrayList<Rectangle2D>();
	
	private void draw(List<BarPrimaryUiUnit> uiUnits) {
		super.update();
		
		float height = com.sun.javafx.tk.Toolkit.getToolkit().getFontLoader().getFontMetrics(gc.getFont()).getLineHeight();
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			BarPrimaryUiUnit uiUnit = uiUnits.get(index);
			double x = uiUnit.getPlot().getChart().getCenter().getBoundsInParent().getMinX() + uiUnit.getX() + uiUnit.getWidth()/2;
			gc.strokeLine(x, 0, x, getHeight() - height);
		}
	}
	
	private void draw(List<BarPrimaryUiUnit> uiUnits, int field) {
		int previousValue = -1;
		Calendar cal = Calendar.getInstance();
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			
			BarPrimaryUiUnit uiUnit = uiUnits.get(index);
			Date date = uiUnit.getModel().getDate();
			double x = uiUnit.getPlot().getChart().getCenter().getBoundsInParent().getMinX()+ uiUnit.getX() + uiUnit.getWidth()/2;
			cal.setTime(date);
			cal.get(field);
			if(previousValue != cal.get(field)) {
				String text = getText(cal, field);
				double width = com.sun.javafx.tk.Toolkit.getToolkit().getFontLoader().computeStringWidth(text, gc.getFont());
				Rectangle2D rectangle = new Rectangle2D(x-width/2-AbstractHorizontalAxis.padding, 0, width+AbstractHorizontalAxis.padding*2, getHeight());
				boolean intersects = false;
				for(Rectangle2D r : rectangles) {
					if(rectangle.intersects(r)) {
						intersects = true;
						break;
					}
				}
				
				if(!intersects) {
					rectangles.add(rectangle);
					gc.strokeText(text, x, getHeight());
					previousValue = cal.get(field);
				}
				
			}
		}
	}
	
	private String getText(Calendar cal, int field) {
		switch(field) {
		case Calendar.YEAR : 
			return String.valueOf(cal.get(field));
		case Calendar.MONTH : 
			return months[cal.get(field)];
		case Calendar.DATE : 
			return String.valueOf(cal.get(field));
		}
		return "";
	}
	
	public void update() {
		rectangles.clear();
		if(getChart().getFrontPlot() instanceof BarPrimaryPlot) {
			BarPrimaryPlot barPrimaryPlot = (BarPrimaryPlot)getChart().getFrontPlot();
			if(null != barPrimaryPlot && null != barPrimaryPlot.getUiUnits() && !barPrimaryPlot.getUiUnits().isEmpty()) {
				List<BarPrimaryUiUnit> uiUnits = barPrimaryPlot.getUiUnits();
				draw(uiUnits);
				draw(uiUnits, Calendar.YEAR);
				draw(uiUnits, Calendar.MONTH);
				draw(uiUnits, Calendar.DATE);
			}
		}
	}

}

